"""
File: example_2.py
Project: 2_猜猜演讲者的秘密
File Created: 2020/02/10 22:33:46
Author: Dunmore (fovd2020@outlook.com)
-----
Last Modified:
Modified By:
-----
Description:
1.1 思考与练习 提高部分 2.猜猜演讲者的秘密
利用Python中的wordcloud词云库来提取乔布斯演讲的关键词
"""

import os
import numpy as np
from PIL import Image
from wordcloud import WordCloud

# 获取此文件的绝对路径
current_dir = (
    os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()
)


# 读取文件中的重庆景点，如果是已经整理过的列表可以无须结巴分词，直接传递给wordcloud生成词云
wc_text = open(
    os.path.join(current_dir, "steve_jobs.txt"), "r", encoding="utf-8"
).read()

# 指定遮罩图片
apple_mask = np.array(Image.open(os.path.join(current_dir, "apple_logo.png")))

# 创建词云对象
word_cloud = WordCloud(background_color="white", mask=apple_mask)
word_cloud.generate(wc_text)

# 展示词云图片
image = word_cloud.to_image()
image.show()

# 生成图片文件
# word_cloud.to_file("t.png")
